package io.tij;

import java.io.Externalizable;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;

import static util.Print.*;

class Blip1 implements Externalizable {

	public Blip1() {
		print(this.getClass().getName() + " Constructor");
	}

	public void readExternal(ObjectInput in) throws IOException,
			ClassNotFoundException {
		print(this.getClass().getName() + ".readExternal");
	}

	public void writeExternal(ObjectOutput out) throws IOException {
		print(this.getClass().getName() + ".writeExternal");
	}

}

class Blip2 implements Externalizable {

	Blip2() {
		print(this.getClass().getName() + " Constructor");
	}

	public void readExternal(ObjectInput in) throws IOException,
			ClassNotFoundException {
		print(this.getClass().getName() + ".readExternal");
	}

	public void writeExternal(ObjectOutput out) throws IOException {
		print(this.getClass().getName() + ".writeExternal");
	}

}

public class Blips {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws IOException,
			ClassNotFoundException {

		String filepath = "Blips.out";

		ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(
				filepath));
		Blip1 blip1 = new Blip1();
		Blip2 blip2 = new Blip2();

		print("Write Objects");
		out.writeObject(blip1);
		out.writeObject(blip2);
		out.close();

		ObjectInputStream in = new ObjectInputStream(new FileInputStream(
				filepath));
		print("Read Objects");
		Blip1 b1 = (Blip1) in.readObject();
		// Cann't deserialization, because Blip2's constructor is not public.
		// Blip2 b2 = (Blip2) in.readObject();
		in.close();
	}
}
/*
 * Output:
io.Blip1 Constructor
io.Blip2 Constructor
Write Objects
io.Blip1.writeExternal
io.Blip2.writeExternal
Read Objects
io.Blip1 Constructor
io.Blip1.readExternal
 */
